@extends('layouts.app')

@section('title', '资源下载')

@section('header')
    @include('layouts.header-two')
@stop

@section('style')
    <style>
        .pagination>.active>span {background-color: #D33829; border-color: #D33829}
        .pagination>.active>span:hover {background-color: #D33829; border-color: #D33829}
        .pagination > li > a, .pagination > li > span { color:#444}
    </style>
@stop

@section('content')
    <div class="search-bar">
        <div class="container">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <form>
                        <div class="input-group">
                            <input name="keyword" type="text" class="form-control" placeholder="请输入您要查找的资源名称" value="{{ isset($param['keyword']) ? $param['keyword'] : '' }}">
                            <span class="input-group-btn">
                            <button class="btn search" type="submit">搜索</button>
                        </span>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="search-section">
        <div class="container">
            <div class="search-section-wrapper">
                <div class="row">
                    <div class="col-md-2">
                        <div class="search-section-ls">
                            <h2>产品分类</h2>
                            <div class="search-sidebar">
                                @foreach($categorys as $category)
                                    <div class="sidebar-section">
                                        @if(count($category->children) <= 0)
                                            <a href="{{ route('resourceIndex', ['category_id' => $category->id, 'keyword' => isset($param['keyword']) ? $param['keyword'] : '']) }}"><h3>{{ $category->name }}</h3></a>
                                        @else
                                            <h3>{{ $category->name }}</h3>
                                        @endif
                                        <ul>
                                            @foreach($category->children as $child)
                                                <li @if($categoryId == $child->id) class="ons" @endif>
                                                    <a href="{{ route('resourceIndex', ['category_id' => $child->id, 'keyword' => isset($param['keyword']) ? $param['keyword'] : '']) }}">{{ $child->name }}</a>
                                                </li>
                                            @endforeach
                                        </ul>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                    <div class="col-md-10">
                        <div class="search-section-rs download-page">
                            @if($filter)
                                <div class="search-cate">
                                    @foreach($filter as $key => $value)
                                        <div class="cate_detail_item">
                                            @if($specialFilter)
                                                <a class="cate_detail_tit">分类：</a>
                                            @else
                                                <a href="{{ route('resourceIndex',array_merge($param, ['L' => $value->id])) }}" class="cate_detail_tit">{{ $value->name }}：</a>
                                            @endif

                                            @foreach($value->children as $k => $child)
                                                <a href="{{ route('resourceIndex', ['category_id' => $categoryId, 'C' => $param['C'] . ',' . $child->id, 'keyword' => isset($param['keyword']) ? $param['keyword'] : '']) }}"
                                                   class="cate_detail_con @if(in_array($child->id, $selectArr)) actives @endif">
                                                    {{ $child->name }}
                                                </a>
                                            @endforeach
                                        </div>
                                        @if(isset($value->grades))
                                            <div class="cate_detail_item">
                                                <a class="cate_detail_tit">年级：</a>
                                                @foreach($value->grades as $k => $grade)
                                                    <a href="{{ route('resourceIndex', ['category_id' => $categoryId, 'C' => $param['C'] . ',' . $grade->id, 'keyword' => isset($param['keyword']) ? $param['keyword'] : '']) }}"
                                                       class="cate_detail_con @if(in_array($grade->id, $selectArr)) actives @endif">
                                                        {{ $grade->name }}
                                                    </a>
                                                @endforeach
                                            </div>
                                        @endif
                                    @endforeach
                                </div>
                            @endif
                            <div class="deep-search">
                                <div class="row">
                                    <div class="col-md-6">
                                        <ul class="deep-search-l">
                                            <li @if(!isset($param['cost']) || !$param['cost']) class="active" @endif>
                                                <a href="{{ route('resourceIndex', array_merge($param, ['cost' => ''])) }}">全部</a>
                                            </li>
                                            <li @if(isset($param['cost']) && $param['cost'] == 'free') class="active" @endif>
                                                <a href="{{ route('resourceIndex', array_merge($param, ['cost' => 'free'])) }}">免费</a>
                                            </li>
                                            <li @if(isset($param['cost']) && $param['cost'] == 'charge')) class="active" @endif>
                                                <a href="{{ route('resourceIndex', array_merge($param, ['cost' => 'charge'])) }}">收费</a>
                                            </li>
                                            @if(Auth::check())
                                                <li @if(isset($param['cost']) && $param['cost'] == 'bought')) class="active" @endif>
                                                    <a href="{{ route('resourceIndex', array_merge($param, ['cost' => 'bought'])) }}">已购</a>
                                                </li>
                                            @endif
                                        </ul>
                                    </div>
                                    <div class="col-md-6">
                                        <ul class="deep-search-r text-right">
                                            <li @if(!isset($param['sort']) || !$param['sort']) class="active" @endif>
                                                <a href="{{ route('resourceIndex', array_merge($param, ['sort' => ''])) }}">默认排序</a>
                                            </li>
                                            <li @if(isset($param['sort']) && $param['sort'] == 'new')) class="active" @endif>
                                                <a href="{{ route('resourceIndex', array_merge($param, ['sort' => 'new'])) }}">最新发布</a>
                                            </li>
                                            <li @if(isset($param['sort']) && $param['sort'] == 'hot')) class="active" @endif>
                                                <a href="{{ route('resourceIndex', array_merge($param, ['sort' => 'hot'])) }}">最受欢迎</a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="search-tab">
                                @foreach($files as $key => $file)
                                <div class="download-item">
                                    <div class="row">
                                        <div class="col-sm-2">
                                            <a target="_blank" href="{{ route('productInfo', ['uuid' => $file->product->uuid]) }}">
                                                <div class="download-item-img">
                                                    <img originalSrc="@if($file->product->cover){{ asset('uploads/' . $file->product->cover[0]) }} @endif" alt="" />
                                                </div>
                                            </a>
                                        </div>
                                        <div class="col-sm-7">
                                            <div class="download-content">
                                                <a target="_blank" href="{{ route('productInfo', ['uuid' => $file->product->uuid]) }}">
                                                    <h3>{{ $file->title }}</h3>
                                                </a>
                                                <p>{{ $file->product->summary }} &nbsp;- -&nbsp; {{ $file->product->name }}</p>
                                                <div class="download-item-msg clearfix">
                                                    <div class="msg-l f_left">
                                                            @if($file->category == 'video')
                                                                <img src="{{ asset('statics/images/icon_video_download.png') }}" alt="视频资源" />
                                                            @elseif($file->category == 'word')
                                                                <img src="{{ asset('statics/images/icon_word_download.png') }}" alt="Word文档" />
                                                            @elseif($file->category == 'pdf')
                                                                <img src="{{ asset('statics/images/icon_pdf_download.png') }}" alt="PDF文档" />
                                                            @elseif($file->category == 'audio')
                                                                <img src="{{ asset('statics/images/icon_music_download.png') }}" alt="音频资源" />
                                                            @endif
                                                        <span class="size">文件大小：</span>
                                                        <span class="size-num">@if($file->size) {{ round($file->size / 1024 /1024, 2) }} @else 0 @endif &nbsp;Mb</span>
                                                    </div>
                                                    <div class="msg-r f-right">
                                                        {{--<i class="collect-num @if($file->is_star == true) active @endif" style="cursor: pointer">{{ $file->star }}</i>--}}
                                                        <i class="download-num">{{ $file->download }}</i>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-sm-3">
                                            <div class="download-rside">
                                                <div class="download-text">
                                                    <i class="golds"></i>
                                                    <p>
                                                        @if($file->is_bought == true)
                                                            已购
                                                        @elseif($file->cost == 'free')
                                                            免费
                                                        @else
                                                            {{ $file->price }}金币
                                                        @endif
                                                    </p>
                                                    <a class="download-btn" style="cursor: pointer" data-file-id="{{ $file->id }}" data-price="@if($file->cost == 'free')0 @else {{ $file->price }} @endif" data-bought="{{ $file->is_bought }}" data-cost="{{ $file->cost }}">下载</a>
                                                </div>
                                                <div class="progress-tab">
                                                    <p></p>
                                                    <div class="progress">
                                                        <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"></div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                @endforeach

                                <div class="page">
                                    {{--<ul class="pageline">--}}
                                        {{--<li class="preDefault"><a href="#"></a></li>--}}
                                        {{--<li class="choose"><a href="#">1</a></li>--}}
                                        {{--<li><a href="#">2</a></li>--}}
                                        {{--<li><a href="#">3</a></li>--}}
                                        {{--<li><a href="#">4</a></li>--}}
                                        {{--<li><a href="#">5</a></li>--}}
                                        {{--<li class="next"><a href="#"></a></li>--}}
                                    {{--</ul>--}}
                                    {!! $files->appends($param)->render() !!}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@stop

@section('footer')
    @include('layouts.footer')

    <!--------------------余额不足-------------------->
    <div class="on-recharge coin-lack">
        <div class="recharge-bg"></div>
        <div class="recharge-feeback gold-tips">
            <h3>余额不足</h3>
            <p class="my-gold"><span>我的金币：<span class="my-coin"></span></span><i class="golds"></i></p>
            <p class="need-gold">需要金币：<span class="need-coin"></span>金币</p>
            <div class="recharge-feeback-btn">
                <a href="{{ route('payment') }}" class="finish">去充值</a>
                <a href="javascript:void(0);" class="unfinished">取消</a>
            </div>
        </div>
    </div>
    <!--------------------消费金币下载-------------------->
    <div class="on-recharge buy-confirm">
        <div class="recharge-bg"></div>
        <div class="recharge-feeback gold-tips gold-tips1">
            <h3>消费<span class="expend-coin"></span>金币下载</h3>
            <p class="my-gold"><span>我的金币：<spanp class="my-coin"></spanp></span><i class="golds"></i></p>
            <div class="recharge-feeback-btn">
                <input type="hidden" id="file-id" value="">
                <a href="javascript:void(0);" class="finish confirm-download">下载</a>
                <a href="javascript:void(0);" class="unfinished">取消</a>
            </div>
        </div>
    </div>
@stop

@section('script')
    <script type="text/javascript" src="{{ asset('statics/js/lyz.delayLoading.min.js') }}" ></script>
    <script>
        $(".sidebar-section").eq(1).find("h3").css({"background-position":"0px -147px"});
        $(".sidebar-section").eq(2).find("h3").css({"background-position":"0px -184px"});

        $(function(){
            var h1=$(".search-section-rs").height();
            var h2=$(".search-section-ls").height();
            if(h1>h2){
                $(".search-section-ls").height($(".search-section-rs").height());
            }
        })

        //--------------------图片延迟加载--------------------//
        $(function () {
            $(".download-item-img img").delayLoading({
                defaultImg: "{{ asset('statics/images/loadings.gif') }}",
                errorImg: "",
                imgSrcAttr: "originalSrc",
                beforehand: 0,
                event: "scroll",
                duration: "normal",
                container: window,
                success: function (imgObj) { },
                error: function (imgObj) { }
            });
        });

        $(".unfinished").click(function () {
            $('.on-recharge').hide();
        });

        @if (Auth::check())
        $(".download-btn").click(function () {
            var userCoin = "{{ Auth::user()->coin }}";
            var fileId = $(this).attr('data-file-id');
            var price = $(this).attr('data-price');
            var bought = $(this).attr('data-bought');
            var cost = $(this).attr('data-cost')
            $('.my-coin').text(userCoin);
            $('#file-id').val(fileId);

            if (bought == 1 || cost == 'free')
            {
                $(".confirm-download").click();
            } else if (userCoin < price)
            {
                $('.need-coin').text(price);
                $('.coin-lack').show();
            } else {
                $('.expend-coin').text(price);
                $('.buy-confirm').show();
            }
        })

        $(".confirm-download").click(function () {
            var fileId = $('#file-id').val();
            $.ajax({
                url: '{{ route('resourceDownload') }}',
                type: 'POST',
                dataType: 'json',
                data: {
                    'file_id' : fileId,
                    '_token' : "{{ csrf_token() }}"
                },
                success:function(data){
                    if(data.status == 'error'){
                        if (data.code == 10001)
                        {
                            $('.buy-confirm').hide();
                            $('.my-coin').text(data.data.my_coin);
                            $('.need-coin').text(data.data.need_coin);
                            $('.coin-lack').show();
                        }
                        return ;
                    } else {
                        $('.buy-confirm').hide();
                        window.location.href = data.data.url;
                        // window.open(data.data.url)
                    }
                }
            });
        });
        @else
        $(".download-btn").click(function () {
            window.location.href="{{ route('login') }}"
        });
        @endif
    </script>
@stop